﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;

namespace LimpaGaragem.DAO.Product.Auto
{
    public class Version
    {
        /// <summary>
        /// Return list of version
        /// </summary>
        /// <returns></returns>
        public List<LimpaGaragem.Model.Product.Auto.Version> GetListOfVersion()
        {
            try
            {
                Framework.DataAccess objDataAccess = new Framework.DataAccess();
                DataTable objDataTable = objDataAccess.ReturnDataTable(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString, Framework.DataAccess.QueryType.Procedure, "PR022_ListVersions", 0);

                List<LimpaGaragem.Model.Product.Auto.Version> objListOfVersion = new List<LimpaGaragem.Model.Product.Auto.Version>();
                foreach (DataRow objDataRow in objDataTable.Rows)
                {
                    LimpaGaragem.Model.Product.Auto.Version objVersion = new LimpaGaragem.Model.Product.Auto.Version();
                    objVersion.ID = Convert.ToInt32(objDataRow["ID_VERSION"]);
                    objVersion.IDModel = Convert.ToInt32(objDataRow["ID_MODEL"]);
                    objVersion.Name = objDataRow["NM_VERSION"].ToString();
                    objVersion.Year = Convert.ToInt16(objDataRow["NU_YEAR"]);
                    objListOfVersion.Add(objVersion);
                }
                return objListOfVersion;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Return data of version
        /// </summary>
        /// <returns></returns>
        public LimpaGaragem.Model.Product.Auto.Version GetVersion(int IDVersion)
        {
            try
            {
                Framework.DataAccess objDataAccess = new Framework.DataAccess();
                
                //PARAMETERS
                List<SqlParameter> objListOfParameters = new List<SqlParameter>();
                objListOfParameters.Add(new SqlParameter("@ID_VERSION", IDVersion));
                
                //RETURN DATA
                DataTable objDataTable = objDataAccess.ReturnDataTable(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString, Framework.DataAccess.QueryType.Procedure, "PR022_SelectVersion", 0, objListOfParameters);

                LimpaGaragem.Model.Product.Auto.Version objVersion = null;
                if (objDataTable.Rows.Count > 0)
                {
                    objVersion = new LimpaGaragem.Model.Product.Auto.Version();
                    objVersion.ID = IDVersion;
                    objVersion.IDModel = Convert.ToInt32(objDataTable.Rows[0]["ID_MODEL"]);
                    objVersion.Name = objDataTable.Rows[0]["NM_VERSION"].ToString();
                    objVersion.Year = Convert.ToInt16(objDataTable.Rows[0]["NU_YEAR"]);
                }
                return objVersion;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
